package nc.tb.ufo.ui;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Rectangle;

import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicButtonUI;

public class ToolBarButtonUI extends BasicButtonUI{
    private Color normalC1 = new Color(0xe1eae7);
    private Color normalC2 =new Color(0xacbbed);
    private Color rollC1 = new Color(0xedf2fa);
    private Color rollC2 = new Color(0xced9f5);
    private Color pressC1 = new Color(0xacbbed);
    private Color pressC2 = new Color(0xe1eaf7);
    private Color noenableC1 = new Color(0xf3f3f3);
    private Color noenableC2 = new Color(0xe1e1e1);
    private Color borderC = new Color(0x234464);
    private Color noenalbeBC = new Color(0xa1a192);
    
    private Color focusBC = new Color(0xb83bbfa);
    public void paint(Graphics g, JComponent c) {
        AbstractButton b = (AbstractButton)c;
        if(b.isOpaque())
            paintBackGround((Graphics2D)g, b);
        super.paint(g, c);
    }
  
    protected void installDefaults(AbstractButton b){
        super.installDefaults(b);
        b.setRolloverEnabled(true);
        b.setBorder(null);
        b.setBorderPainted(false);
    }
    protected void paintFocus(Graphics g, AbstractButton b,
              Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
      Color oColor = g.getColor();  
      g.setColor(focusBC);
      g.drawRect(1,1,b.getWidth() -3, b.getHeight() -3);
      g.drawRect(2,2,b.getWidth() -5, b.getHeight() -5);
      g.setColor(oColor);
    }
    private void paintBackGround(Graphics2D g2, AbstractButton b){
        ButtonModel model = b.getModel();
        Color c1 = null;
        Color c2 = null;
        Color bc = borderC;
        if(!model.isEnabled()){
          c1 = noenableC1;
          c2 = noenableC2;
          bc = noenalbeBC;
        }else if(model.isPressed() && model.isArmed()){
          c1 = pressC1;
          c2 = pressC2;     
        }else if(model.isRollover()){
          c1 = rollC1;
          c2 = rollC2;   
        }else{
          c1 = normalC1;
          c2 = normalC2;
        }

        GradientPaint gp = new GradientPaint(0,0,c1,0, b.getHeight(),c2);
        Paint oldPaint = g2.getPaint();
        g2.setPaint(gp);
        g2.fillRect(0,0,b.getWidth(),b.getHeight());
        g2.setColor(bc);
//        g2.drawRect(0,0,b.getWidth() -1, b.getHeight() -1);
        
        g2.setPaint(oldPaint);
    }

}
